package canchaFulbito.servletacceso;

import java.io.IOException;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import canchaFulbito.excepcion.DAOExcepcion;
import canchaFulbito.modelo.Distrito;
import canchaFulbito.negocio.SeguridadNegocio;

/**
 * Servlet implementation class ListarDistritosServlet
 */
@WebServlet("/ListarDistritosServlet")
public class ListarDistritosServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ListarDistritosServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.print("Inicio del Servlet :"+ "\n");
		System.out.print("Inicio del Servlet :"+ "\n");
				RequestDispatcher rd;
				SeguridadNegocio neg = new SeguridadNegocio();
				try {
					Collection<Distrito> encontrados = neg.ListarDistrito();
					System.out.print("Encontrados ListarDistritoServlet : " + encontrados.size()+ "\n");
					if(encontrados.size() > 0){
						
						for (Distrito c : encontrados) {
							HttpSession session = request.getSession();
							session.setAttribute("idDistrito",c.getIdDistrito());
							session.setAttribute("NomDistrito", c.getNomDistrito());
						}
						rd = request.getRequestDispatcher("BuscarCancha.jsp");
						rd.forward(request, response);
					}
					else{
						request.setAttribute("MENSAJE", "No existen Distritos Cargados");
						
					}
				}
				 catch (DAOExcepcion e) {
						System.out.print("Falló: " + e.getMessage());
					}
	}

}
